package org.yf.samples.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yf.samples.dao.IExamScoreDAO;
import org.yf.samples.dao.IExamTestDAO;
import org.yf.samples.dao.IExamTestTypeDAO;
import org.yf.samples.dao.ISubjectDAO;
import org.yf.samples.dao.IUserDAO;
import org.yf.samples.dao.IUserSubjectDAO;
import org.yf.samples.model.ExamScore;
import org.yf.samples.model.ExamScoreVO;
import org.yf.samples.model.ExamStudentVO;
import org.yf.samples.model.ExamSubjectVO;
import org.yf.samples.model.ExamTest;
import org.yf.samples.model.ExamTestType;
import org.yf.samples.model.ExamTestTypeVO;
import org.yf.samples.model.ExamTestVO;
import org.yf.samples.model.ExamUserSubjectVO;
import org.yf.samples.model.PagerModel;
import org.yf.samples.model.Subject;
import org.yf.samples.model.UserSubject;
import org.yf.samples.util.SystemContext;

@Service
@Transactional
public class TeacherServiceImpl implements ITeacherService {
	
	@Autowired
	private IExamTestDAO examTestDAO;
	
	@Autowired
	private ISubjectDAO subjectDAO;
	
	@Autowired
	private IUserSubjectDAO userSubjectDAO;
	
	@Autowired
	private IUserDAO userDAO;
	
	@Autowired
	private IExamTestTypeDAO examTestTypeDAO;
	
	@Autowired
	private IExamScoreDAO examScoreDAO;

	@Override
	public List<ExamTestVO> findAllTests() {
		List<ExamTestVO> voList = new ArrayList<ExamTestVO>();
		List<ExamTest> examTestList = examTestDAO.FindAllExamTests();
		for(ExamTest examTest : examTestList){
			voList.add(getTestById(examTest.getId()));
		}
		return voList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<ExamTestVO> findAllByPage() {
		PagerModel<ExamTestVO> page = new PagerModel<ExamTestVO>();
		ArrayList<ExamTestVO> dates = new ArrayList<ExamTestVO> ();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPageSize();
		List<ExamTest> tempDates = examTestDAO.createQuery(" FROM ExamTest ").setFirstResult(offset).setMaxResults(pageSize).list();
		for(ExamTest examTest : tempDates){
			ExamTestVO examTestVO = new ExamTestVO();
			BeanUtils.copyProperties(examTest, examTestVO);
			Subject subject = examTest.getSubject();
			if(subject != null){
				examTestVO.setSubName(subject.getName());
			}
			
			ExamTestTypeVO typeVO = new ExamTestTypeVO();
			BeanUtils.copyProperties(examTest.getExamTestType(), typeVO);
			examTestVO.setExamTestType(typeVO);
			dates.add(examTestVO);
		}
		page.setDates(dates);
		page.setOffset(offset);
		page.setPageSize(pageSize);//获取总记录数
		long  totalSize  =  (long)  examTestDAO.createQuery("select  count(*)  from  ExamTest ").uniqueResult();
		page.setTotalSize(totalSize);
		return page;
	}
	
	@Override
	public ExamTestVO getTestById(Integer id) {
		ExamTest entity = examTestDAO.getExamTestById(id);
		if(entity != null){
			ExamTestVO vo = new ExamTestVO();
			BeanUtils.copyProperties(entity, vo);
			
			Subject subject = entity.getSubject();
			if(subject != null){
				vo.setSubName(subject.getName());
				vo.setSubId(subject.getSubId());
			}
			
			ExamTestTypeVO typeVO = new ExamTestTypeVO();
			BeanUtils.copyProperties(entity.getExamTestType(), typeVO);
			vo.setExamTestType(typeVO);
			return vo;
		}
		return null;
	}

	@Override
	public Integer editTest(ExamTestVO examTestVO) throws Exception {
		ExamTest examTest = new ExamTest();
		BeanUtils.copyProperties(examTestVO, examTest);
		ExamTestType examTestType = new ExamTestType();
		BeanUtils.copyProperties(examTestVO.getExamTestType(), examTestType);
		examTest.setExamTestType(examTestType);
		Subject subject = subjectDAO.getSubjectById(examTestVO.getSubId());
		examTest.setSubject(subject);
		examTestDAO.editExamTest(examTest);
		return 1;
	}

	@Override
	public Integer createTest(ExamTestVO examTestVO) throws Exception {
		ExamTest examTest = new ExamTest();
		BeanUtils.copyProperties(examTestVO, examTest);
		ExamTestType examTestType = new ExamTestType();
		BeanUtils.copyProperties(examTestVO.getExamTestType(), examTestType);
		examTest.setExamTestType(examTestType);
		Subject subject = subjectDAO.getSubjectById(examTestVO.getSubId());
		examTest.setSubject(subject);
		examTestDAO.createExamTest(examTest);
		return 1;
	}

	@Override
	public Integer deleteTest(Integer id) throws Exception {
		examTestDAO.deleteExamTest(id);
		return 1;
	}

	@Override
	public List<ExamSubjectVO> getSubjectList() {
		List<ExamSubjectVO> voList = new ArrayList<ExamSubjectVO>();
		List<Subject> subjectList = subjectDAO.FindAllSubjects();
		for(Subject subject : subjectList){
			Subject entity = subjectDAO.getSubjectById(subject.getSubId());
			if(entity != null){
				ExamSubjectVO vo = new ExamSubjectVO();
				BeanUtils.copyProperties(entity, vo);
				voList.add(vo);
			}
		}
		return voList;
	}

	@Override
	public List<ExamTestTypeVO> getTestTypeList() {
		List<ExamTestTypeVO> voList = new ArrayList<ExamTestTypeVO>();
		List<ExamTestType> testTypeList = examTestTypeDAO.FindAllExamTestTypes();
		for(ExamTestType testType : testTypeList){
			ExamTestType entity = examTestTypeDAO.getExamTestTypeById(testType.getId());
			if(entity != null){
				ExamTestTypeVO vo = new ExamTestTypeVO();
				BeanUtils.copyProperties(entity, vo);
				voList.add(vo);
			}
		}
		return voList;
	}

	@Override
	public List<ExamStudentVO> getStudentList() {
		List<ExamStudentVO> voList = userDAO.FindUserByStudent();
		for(ExamStudentVO examStudentVO : voList){
			List<Subject> subList = subjectDAO.FindSubjectsByUserId(examStudentVO.getId());
			List<ExamSubjectVO> subVOList = new ArrayList<ExamSubjectVO>();
			for(Subject subject : subList){
				ExamSubjectVO vo = new ExamSubjectVO();
				BeanUtils.copyProperties(subject, vo);
				subVOList.add(vo);
			}
			examStudentVO.setSubjectList(subVOList);
		}
		return voList;
	}

	@Override
	public List<ExamUserSubjectVO> getUserSubjectsByUserId(String userId) {
		List<UserSubject> list = userSubjectDAO.getUserSubjectsByUserId(userId);
		List<ExamUserSubjectVO> voList = new ArrayList<ExamUserSubjectVO>();
		for(UserSubject userStudent : list){
			ExamUserSubjectVO vo = new ExamUserSubjectVO();
			BeanUtils.copyProperties(userStudent, vo);
			voList.add(vo);
		}
		return voList;
	}

	@Override
	public List<ExamScoreVO> getScoreList() {
		List<ExamScore> scoreList = examScoreDAO.FindAllScore();
		List<ExamScoreVO> voList = new ArrayList<ExamScoreVO>();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		for(ExamScore score : scoreList){
			ExamScoreVO vo = new ExamScoreVO();
			BeanUtils.copyProperties(score, vo);
			vo.setExamTimeString(df.format(vo.getExamTime()));
			vo.setUserId(score.getUser().getUserId());
			vo.setUserName(score.getUser().getUserName());
			vo.setSubjectName(score.getSubject().getName());
			voList.add(vo);
		}
		return voList;
	}

	@Override
	public List<ExamScoreVO> getScore(String condition, String query) throws Exception {
		List<ExamScore> scoreList = null;
		switch(condition){
			case "sub":
				scoreList = examScoreDAO.getScoreBySub(query);
				break;
			case "id":
				scoreList = examScoreDAO.getScoreByUserId(query);
				break;
			case "name":
				scoreList = examScoreDAO.getScoreByUserName(query);
				break;
			case "date":
				String[] date = query.split(",");
				scoreList = examScoreDAO.getScoreByDate(date);
				break;
		}
		List<ExamScoreVO> voList = new ArrayList<ExamScoreVO>();
		if(scoreList != null){
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
			for(ExamScore score : scoreList){
				ExamScoreVO vo = new ExamScoreVO();
				BeanUtils.copyProperties(score, vo);
				vo.setExamTimeString(df.format(vo.getExamTime()));
				vo.setUserId(score.getUser().getUserId());
				vo.setUserName(score.getUser().getUserName());
				vo.setSubjectName(score.getSubject().getName());
				voList.add(vo);
			}
			return voList;
		}else{
			return null;
		}
	}

}
